package lukeer.fencing;

import csproj.fencing.Move;
import csproj.fencing.Player;

import java.util.ArrayList;

/**
 * Player using basic heuristics over the currentGameState only to produce a move.
 *
 * User: Luke Richardson lukeer, Wafa M'Barek wmbarek
 * Date: 05/10/12
 * Time: 21:09
 */
public class SimpleEvaluationPlayer extends BasicPlayer implements Player {
    private int x, y, fX, fY, direction;

    /**
     * Uses basic heuristics and no look ahead to place moves on the currentGameState board.
     *
     * @return move to be tested by opponent
     */
    @Override
    public Move makeMove() {
        decideFence();
        Move m;
        if (decidePiece()) m = new Move(x, y, player, true, fX, fY, true, direction);
        else {
            m = new Move(-1, -1, player, false, fX, fY, true, direction);
        }
        try {
            currentGameState.placePiece(m);
        } catch (FencingException e) {
            System.err.println("Illegal move generated by SimpleEvaluation player");
            System.exit(1);
        }
        return m;
    }

    /**
     * Decides fence placement by randomly selecting an available fence
     */
    private void decideFence() {
        ArrayList<Pair> hFences = currentGameState.getEmptyHFences();
        ArrayList<Pair> vFences = currentGameState.getEmptyVFences();
        boolean horz = r.nextBoolean();
        if (horz) {
            Pair p = hFences.get(r.nextInt(hFences.size()));
            fX = p.getX();
            fY = p.getY();
            direction = HORIZONTAL;
        } else {
            Pair p = vFences.get(r.nextInt(vFences.size()));
            fX = p.getX();
            fY = p.getY();
            direction = VERTICAL;
        }

    }

    /**
     * Decides piece by choosing an area that we are most likely to win
     *
     * @return true if a piece can be placed on the current board
     */
    private boolean decidePiece() {
        Sector optimalSector = null;
        int pieceDifference = Integer.MIN_VALUE;
        ArrayList<Sector> sectors = currentGameState.getSectors();
        if (currentGameState.isFull()) return false;
        for (Sector s : sectors) {
            if (!s.isEliminated()) {
                int sectorDifference;
                if (player == BLACK) sectorDifference = s.getBlack() - s.getWhite();
                else {
                    sectorDifference = s.getWhite() - s.getWhite();
                }
                if (sectorDifference >= pieceDifference) {
                    optimalSector = s;
                    pieceDifference = sectorDifference;
                }
            }
        }

        BoardPiece boardPiece;
        do {
            boardPiece = optimalSector.getPiece(r.nextInt(optimalSector.size()));
        } while (boardPiece.getType() != EMPTY);
        x = boardPiece.getX();
        y = boardPiece.getY();
        return true;
    }
}
